{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入需要的请求模块\n",
    "import requests\n",
    "import json\n",
    "# 设置人脸列表ID\n",
    "faceListId =  'faceid_fyf002'\n",
    "# 输入azure的终结点以及密钥\n",
    "create_facelists_url =  'https://api-fyf.cognitiveservices.azure.com/face/v1.0/facelists/{}'# URL（具体参考API文档）\n",
    "subscription_key = 'd27ae176995a44b8986e38bdc382757b'\n",
    "assert subscription_key\n",
    "# 请求headers\n",
    "headers = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "# 相关信息以及识别模型的选用\n",
    "data = {\n",
    "    \"name\": \"api_facelist\",\n",
    "    \"userData\": \"相册集\",\n",
    "    \"recognitionModel\": \"recognition_03\",\n",
    "    \n",
    "}\n",
    "params={\n",
    "    \"faceListId\":\"faceid_fyf002\"\n",
    "}\n",
    "# 发送请求\n",
    "r_create = requests.put(create_facelists_url.format(faceListId),headers=headers,json=data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b''"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_create.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#先加一张脸试试\n",
    "# 2、Add face\n",
    "add_face_url = 'https://api-fyf.cognitiveservices.azure.com/face/v1.0/facelists/{}/persistedfaces'\n",
    "\n",
    "assert subscription_key\n",
    "headers = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "# 人脸相片的地址\n",
    "img_url = \"https://gitee.com/fengyanfen-new/api/raw/master/picture/%E6%B1%AA%E8%8B%8F%E6%B3%B7.jpg\"\n",
    "# 人脸数据\n",
    "params_add_face={\n",
    "    \"faceListId\":\"faceid_fyf002\",\n",
    "    \"userData\":\"相册集\"\n",
    "}\n",
    "# 发送请求\n",
    "r_add_face = requests.post(add_face_url.format(faceListId),headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "r_add_face.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaceId': '91473781-2c73-40d4-88b5-8ebe1003ea4c'}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_add_face.json()#返回persistedFaceId"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 封装成函数方便添加图片\n",
    "def AddFace(img_url=str,userData=str):\n",
    "    add_face_url =\"https://api-fyf.cognitiveservices.azure.com/face/v1.0/facelists/{}/persistedFaces\"\n",
    "    assert subscription_key\n",
    "    headers = {\n",
    "        # Request headers\n",
    "        'Content-Type': 'application/json',\n",
    "        'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "    }\n",
    "    img_url = img_url\n",
    "\n",
    "    params_add_face={\n",
    "        \"userData\":userData\n",
    "    }\n",
    "    r_add_face = requests.post(add_face_url.format(faceListId),headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "    return r_add_face.status_code#返回出状态码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AddFace(\"https://gitee.com/fengyanfen-new/api/raw/master/picture/800.jpg\",\"黄宗泽\")\n",
    "AddFace(\"https://gitee.com/fengyanfen-new/api/raw/master/picture/lx1.jpg\",\"李现\")\n",
    "AddFace(\"https://gitee.com/fengyanfen-new/api/raw/master/picture/pakho.jpg\",\"周柏豪\")\n",
    "AddFace(\"https://gitee.com/fengyanfen-new/api/raw/master/picture/%E9%99%88%E4%BC%9F%E9%9C%86.jpeg\",\"陈伟霆\")\n",
    "AddFace(\"https://gitee.com/fengyanfen-new/api/raw/master/picture/%E5%94%90%E8%89%BA%E6%98%95.jpeg\",\"唐艺昕\")\n",
    "AddFace(\"https://gitee.com/fengyanfen-new/api/raw/master/picture/wlk.jpg\",\"王琳凯\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/Zoezhouyu.jpg\",\"周雨\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/crayon-heimi.jpg\",\"刘瑜鹏\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/jiayichen.jpg\",\"陈嘉仪\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/kg2000.jpg\",\"徐旖芊\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/liuxinrujiayou.jpg\",\"刘心如\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': 'ef1ee297-aea7-45cd-b7a0-726c9a8955cb',\n",
       "  'faceRectangle': {'top': 318, 'left': 152, 'width': 275, 'height': 275},\n",
       "  'faceAttributes': {'smile': 0.02,\n",
       "   'headPose': {'pitch': -6.2, 'roll': -24.0, 'yaw': -13.9},\n",
       "   'gender': 'male',\n",
       "   'age': 20.0,\n",
       "   'facialHair': {'moustache': 0.1, 'beard': 0.1, 'sideburns': 0.1},\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.02,\n",
       "    'neutral': 0.978,\n",
       "    'sadness': 0.002,\n",
       "    'surprise': 0.0},\n",
       "   'blur': {'blurLevel': 'low', 'value': 0.0},\n",
       "   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.74},\n",
       "   'noise': {'noiseLevel': 'low', 'value': 0.15},\n",
       "   'makeup': {'eyeMakeup': True, 'lipMakeup': True},\n",
       "   'accessories': [],\n",
       "   'occlusion': {'foreheadOccluded': False,\n",
       "    'eyeOccluded': False,\n",
       "    'mouthOccluded': False},\n",
       "   'hair': {'bald': 0.03,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'brown', 'confidence': 1.0},\n",
       "     {'color': 'blond', 'confidence': 0.53},\n",
       "     {'color': 'black', 'confidence': 0.47},\n",
       "     {'color': 'red', 'confidence': 0.28},\n",
       "     {'color': 'gray', 'confidence': 0.11},\n",
       "     {'color': 'other', 'confidence': 0.03},\n",
       "     {'color': 'white', 'confidence': 0.0}]}}}]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3、检测人脸的id\n",
    "# replace <My Endpoint String> with the string from your endpoint URL\n",
    "face_api_url = 'https://api-fyf.cognitiveservices.azure.com//face/v1.0/detect'\n",
    "\n",
    "# 请求正文\n",
    "image_url = 'https://gitee.com/fengyanfen-new/api/raw/master/picture/%E6%B1%AA%E8%8B%8F%E6%B3%B7.jpg'\n",
    "\n",
    "headers = {'Ocp-Apim-Subscription-Key': subscription_key}\n",
    "\n",
    "# 请求参数\n",
    "params = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'false',\n",
    "    # 选择model\n",
    "    'recognitionModel':'recognition_03',#此参数需与facelist参数一致\n",
    "    'detectionModel':'detection_01',\n",
    "    # 可选参数,请仔细阅读API文档\n",
    "    'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',\n",
    "}\n",
    "\n",
    "response = requests.post(face_api_url, params=params,\n",
    "                         headers=headers, json={\"url\": image_url})\n",
    "# json.dumps 将json--->字符串\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "findsimilars_url = \"https://api-fyf.cognitiveservices.azure.com/face/v1.0/findsimilars\"\n",
    "\n",
    "# 请求正文 faceId需要先检测一张照片获取\n",
    "data_findsimilars = {\n",
    "    \"faceId\":\"ef1ee297-aea7-45cd-b7a0-726c9a8955cb\",#取上方的faceID\n",
    "    \"faceListId\": \"faceid_fyf002\",\n",
    "    \"maxNumOfCandidatesReturned\": 10,\n",
    "    \"mode\": \"matchFace\"#matchPerson #一种为验证模式，一种为相似值模式\n",
    "    }\n",
    "\n",
    "r_findsimilars = requests.post(findsimilars_url,headers=headers,json=data_findsimilars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_findsimilars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'persistedFaceId': '91473781-2c73-40d4-88b5-8ebe1003ea4c',\n",
       "  'confidence': 1.0},\n",
       " {'persistedFaceId': '584d1986-1d53-4425-a86e-20e3a6d6b76d',\n",
       "  'confidence': 0.1007},\n",
       " {'persistedFaceId': '1bf777fb-8cb5-4203-bad7-9456ec9fcc5c',\n",
       "  'confidence': 0.09895},\n",
       " {'persistedFaceId': '998dbb6d-871d-4c56-b80b-08f3f0d4e087',\n",
       "  'confidence': 0.09687},\n",
       " {'persistedFaceId': 'a8223e84-1c48-4067-8a2c-3322d6fdfe7c',\n",
       "  'confidence': 0.09631},\n",
       " {'persistedFaceId': 'b9aacd84-1307-42be-a817-a5ce54711006',\n",
       "  'confidence': 0.09542},\n",
       " {'persistedFaceId': '3e62558a-c01e-42d1-b88b-7185381b1bbc',\n",
       "  'confidence': 0.09246},\n",
       " {'persistedFaceId': '306d0d07-f22e-44c6-bafc-cbb541e3c8bd',\n",
       "  'confidence': 0.09196},\n",
       " {'persistedFaceId': 'f322d6c1-c8f5-4696-a1aa-49ffee098f48',\n",
       "  'confidence': 0.08844},\n",
       " {'persistedFaceId': 'bf621df1-998e-4489-8b4f-14fc74e75263',\n",
       "  'confidence': 0.08675}]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_findsimilars.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaces': [{'persistedFaceId': '91473781-2c73-40d4-88b5-8ebe1003ea4c',\n",
       "   'userData': '相册集'},\n",
       "  {'persistedFaceId': '3e62558a-c01e-42d1-b88b-7185381b1bbc',\n",
       "   'userData': '黄宗泽'},\n",
       "  {'persistedFaceId': 'f322d6c1-c8f5-4696-a1aa-49ffee098f48',\n",
       "   'userData': '李现'},\n",
       "  {'persistedFaceId': '09e3aab2-98b2-4945-a347-8d637da70c46',\n",
       "   'userData': '周柏豪'},\n",
       "  {'persistedFaceId': '998dbb6d-871d-4c56-b80b-08f3f0d4e087',\n",
       "   'userData': '陈伟霆'},\n",
       "  {'persistedFaceId': 'bf621df1-998e-4489-8b4f-14fc74e75263',\n",
       "   'userData': '唐艺昕'},\n",
       "  {'persistedFaceId': '584d1986-1d53-4425-a86e-20e3a6d6b76d',\n",
       "   'userData': '王琳凯'},\n",
       "  {'persistedFaceId': '306d0d07-f22e-44c6-bafc-cbb541e3c8bd',\n",
       "   'userData': '周雨'},\n",
       "  {'persistedFaceId': '44aac104-3854-4c26-a3d5-8ee0db35c53a',\n",
       "   'userData': '刘瑜鹏'},\n",
       "  {'persistedFaceId': 'a8223e84-1c48-4067-8a2c-3322d6fdfe7c',\n",
       "   'userData': '陈嘉仪'},\n",
       "  {'persistedFaceId': '1bf777fb-8cb5-4203-bad7-9456ec9fcc5c',\n",
       "   'userData': '徐旖芊'},\n",
       "  {'persistedFaceId': 'b9aacd84-1307-42be-a817-a5ce54711006',\n",
       "   'userData': '刘心如'}],\n",
       " 'faceListId': 'faceid_fyf002',\n",
       " 'name': 'api_facelist',\n",
       " 'userData': '相册集'}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Get facelist\n",
    "get_facelist_url = \"https://api-fyf.cognitiveservices.azure.com/face/v1.0/facelists/{}\"#学生填写\n",
    "r_get_facelist = requests.get(get_facelist_url.format(faceListId),headers=headers,params=data)#学生填写\n",
    "r_get_facelist.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>userData</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>91473781-2c73-40d4-88b5-8ebe1003ea4c</td>\n",
       "      <td>相册集</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3e62558a-c01e-42d1-b88b-7185381b1bbc</td>\n",
       "      <td>黄宗泽</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>f322d6c1-c8f5-4696-a1aa-49ffee098f48</td>\n",
       "      <td>李现</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>09e3aab2-98b2-4945-a347-8d637da70c46</td>\n",
       "      <td>周柏豪</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>998dbb6d-871d-4c56-b80b-08f3f0d4e087</td>\n",
       "      <td>陈伟霆</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>bf621df1-998e-4489-8b4f-14fc74e75263</td>\n",
       "      <td>唐艺昕</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>584d1986-1d53-4425-a86e-20e3a6d6b76d</td>\n",
       "      <td>王琳凯</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>306d0d07-f22e-44c6-bafc-cbb541e3c8bd</td>\n",
       "      <td>周雨</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>44aac104-3854-4c26-a3d5-8ee0db35c53a</td>\n",
       "      <td>刘瑜鹏</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>a8223e84-1c48-4067-8a2c-3322d6fdfe7c</td>\n",
       "      <td>陈嘉仪</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1bf777fb-8cb5-4203-bad7-9456ec9fcc5c</td>\n",
       "      <td>徐旖芊</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>b9aacd84-1307-42be-a817-a5ce54711006</td>\n",
       "      <td>刘心如</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         persistedFaceId userData\n",
       "0   91473781-2c73-40d4-88b5-8ebe1003ea4c      相册集\n",
       "1   3e62558a-c01e-42d1-b88b-7185381b1bbc      黄宗泽\n",
       "2   f322d6c1-c8f5-4696-a1aa-49ffee098f48       李现\n",
       "3   09e3aab2-98b2-4945-a347-8d637da70c46      周柏豪\n",
       "4   998dbb6d-871d-4c56-b80b-08f3f0d4e087      陈伟霆\n",
       "5   bf621df1-998e-4489-8b4f-14fc74e75263      唐艺昕\n",
       "6   584d1986-1d53-4425-a86e-20e3a6d6b76d      王琳凯\n",
       "7   306d0d07-f22e-44c6-bafc-cbb541e3c8bd       周雨\n",
       "8   44aac104-3854-4c26-a3d5-8ee0db35c53a      刘瑜鹏\n",
       "9   a8223e84-1c48-4067-8a2c-3322d6fdfe7c      陈嘉仪\n",
       "10  1bf777fb-8cb5-4203-bad7-9456ec9fcc5c      徐旖芊\n",
       "11  b9aacd84-1307-42be-a817-a5ce54711006      刘心如"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#facelist里面的数据\n",
    "import pandas as pd\n",
    "adf = pd.json_normalize(r_get_facelist.json()[\"persistedFaces\"])# 升级pandas才能运行\n",
    "adf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>confidence</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>91473781-2c73-40d4-88b5-8ebe1003ea4c</td>\n",
       "      <td>1.00000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>584d1986-1d53-4425-a86e-20e3a6d6b76d</td>\n",
       "      <td>0.10070</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1bf777fb-8cb5-4203-bad7-9456ec9fcc5c</td>\n",
       "      <td>0.09895</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>998dbb6d-871d-4c56-b80b-08f3f0d4e087</td>\n",
       "      <td>0.09687</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>a8223e84-1c48-4067-8a2c-3322d6fdfe7c</td>\n",
       "      <td>0.09631</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>b9aacd84-1307-42be-a817-a5ce54711006</td>\n",
       "      <td>0.09542</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>3e62558a-c01e-42d1-b88b-7185381b1bbc</td>\n",
       "      <td>0.09246</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>306d0d07-f22e-44c6-bafc-cbb541e3c8bd</td>\n",
       "      <td>0.09196</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>f322d6c1-c8f5-4696-a1aa-49ffee098f48</td>\n",
       "      <td>0.08844</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>bf621df1-998e-4489-8b4f-14fc74e75263</td>\n",
       "      <td>0.08675</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        persistedFaceId  confidence\n",
       "0  91473781-2c73-40d4-88b5-8ebe1003ea4c     1.00000\n",
       "1  584d1986-1d53-4425-a86e-20e3a6d6b76d     0.10070\n",
       "2  1bf777fb-8cb5-4203-bad7-9456ec9fcc5c     0.09895\n",
       "3  998dbb6d-871d-4c56-b80b-08f3f0d4e087     0.09687\n",
       "4  a8223e84-1c48-4067-8a2c-3322d6fdfe7c     0.09631\n",
       "5  b9aacd84-1307-42be-a817-a5ce54711006     0.09542\n",
       "6  3e62558a-c01e-42d1-b88b-7185381b1bbc     0.09246\n",
       "7  306d0d07-f22e-44c6-bafc-cbb541e3c8bd     0.09196\n",
       "8  f322d6c1-c8f5-4696-a1aa-49ffee098f48     0.08844\n",
       "9  bf621df1-998e-4489-8b4f-14fc74e75263     0.08675"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 返回相似度的数据\n",
    "bdf = pd.json_normalize(r_findsimilars.json())# 升级pandas才能运行\n",
    "bdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>userData</th>\n",
       "      <th>confidence</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>91473781-2c73-40d4-88b5-8ebe1003ea4c</td>\n",
       "      <td>相册集</td>\n",
       "      <td>1.00000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>584d1986-1d53-4425-a86e-20e3a6d6b76d</td>\n",
       "      <td>王琳凯</td>\n",
       "      <td>0.10070</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1bf777fb-8cb5-4203-bad7-9456ec9fcc5c</td>\n",
       "      <td>徐旖芊</td>\n",
       "      <td>0.09895</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>998dbb6d-871d-4c56-b80b-08f3f0d4e087</td>\n",
       "      <td>陈伟霆</td>\n",
       "      <td>0.09687</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>a8223e84-1c48-4067-8a2c-3322d6fdfe7c</td>\n",
       "      <td>陈嘉仪</td>\n",
       "      <td>0.09631</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>b9aacd84-1307-42be-a817-a5ce54711006</td>\n",
       "      <td>刘心如</td>\n",
       "      <td>0.09542</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3e62558a-c01e-42d1-b88b-7185381b1bbc</td>\n",
       "      <td>黄宗泽</td>\n",
       "      <td>0.09246</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>306d0d07-f22e-44c6-bafc-cbb541e3c8bd</td>\n",
       "      <td>周雨</td>\n",
       "      <td>0.09196</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>f322d6c1-c8f5-4696-a1aa-49ffee098f48</td>\n",
       "      <td>李现</td>\n",
       "      <td>0.08844</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>bf621df1-998e-4489-8b4f-14fc74e75263</td>\n",
       "      <td>唐艺昕</td>\n",
       "      <td>0.08675</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        persistedFaceId userData  confidence\n",
       "0  91473781-2c73-40d4-88b5-8ebe1003ea4c      相册集     1.00000\n",
       "5  584d1986-1d53-4425-a86e-20e3a6d6b76d      王琳凯     0.10070\n",
       "8  1bf777fb-8cb5-4203-bad7-9456ec9fcc5c      徐旖芊     0.09895\n",
       "3  998dbb6d-871d-4c56-b80b-08f3f0d4e087      陈伟霆     0.09687\n",
       "7  a8223e84-1c48-4067-8a2c-3322d6fdfe7c      陈嘉仪     0.09631\n",
       "9  b9aacd84-1307-42be-a817-a5ce54711006      刘心如     0.09542\n",
       "1  3e62558a-c01e-42d1-b88b-7185381b1bbc      黄宗泽     0.09246\n",
       "6  306d0d07-f22e-44c6-bafc-cbb541e3c8bd       周雨     0.09196\n",
       "2  f322d6c1-c8f5-4696-a1aa-49ffee098f48       李现     0.08844\n",
       "4  bf621df1-998e-4489-8b4f-14fc74e75263      唐艺昕     0.08675"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#合并在一起，得出班级能谁最像你\n",
    "pd.merge(adf, bdf,how='inner', on='persistedFaceId').sort_values(by=\"confidence\",ascending = False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "FacelistId =\"faceid_fyf002\"#学生填写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Detect face 删除列表内人脸id\n",
    "delete_face_url =\"https://api-fyf.cognitiveservices.azure.com/face/v1.0/facelists/faceid_fyf002/persistedfaces/b9aacd84-1307-42be-a817-a5ce54711006\"#学生填写\n",
    "\n",
    "assert subscription_key\n",
    "persistedFaceId = r_add_face.json()[\"persistedFaceId\"]\n",
    "# 直接取上面获得的ID{'persistedFaceId': 'f18450d3-60d2-45f3-a69e-783574dc3ce8'} \n",
    "\n",
    "headers = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "\n",
    "# 注意requests请求为delete\n",
    "r_delete_face = requests.delete(delete_face_url.format(faceListId,persistedFaceId),headers=headers)#学生填写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_delete_face"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Detect face 删除列表内人脸列表\n",
    "\n",
    "delete_facelist_url =\"https://api-fyf.cognitiveservices.azure.com/face/v1.0/facelists/faceid_fyf002\"#学生填写\n",
    "assert subscription_key\n",
    "\n",
    "headers = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "\n",
    "# format内直接删除\n",
    "# 注意requests请求为delete\n",
    "r_delete_facelist = requests.delete(delete_facelist_url.format(faceListId),headers=headers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_delete_facelist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
